package org.west.sky.frame.activiti.study.chapter17;

import de.odysseus.el.util.SimpleContext;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.el.ExpressionFactoryImpl;
import org.west.sky.frame.activiti.study.util.ActivitiEngineUtil;

import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.xml.crypto.Data;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * author: chz
 * date: 2025/6/6
 * description: 内嵌子流程
 */
public class RunDemo17_1_1 extends ActivitiEngineUtil {

    public static void main(String[] args) {
        RunDemo17_1_1 runDemo16_1_2 = new RunDemo17_1_1();
        runDemo16_1_2.run();
    }

    public void run() {
        loadActivitiConfigAndInitEngine("activiti.job.xml");

//        ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
//        SimpleContext simpleContext = new SimpleContext();
//        simpleContext.setVariable("applyDate", expressionFactory.createValueExpression(LocalDate.now(), LocalDate.class));
//        simpleContext.setVariable("heatingYear", expressionFactory.createValueExpression("2024-2025", String.class));
//        simpleContext.setVariable("applyDate2", expressionFactory.createValueExpression("2025-09-15", String.class));
//        ValueExpression valueExpression = expressionFactory.createValueExpression(simpleContext, "${applyDate2 >= heatingYear.split('-')[1].concat('-09-15')}", Boolean.class);
////        ValueExpression valueExpression = expressionFactory.createValueExpression(simpleContext, "${applyDate.isAfter(LocalDate.parse('2025-06-15'))}", Boolean.class);
//        System.out.println("===="+valueExpression.getValue(simpleContext));


        ProcessDefinition processDefinition = deployByClassPathResource("processes/chapter17/Chapter17_1_1.bpmn20.xml");

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task1.getName() + "：任务节点");
        taskService.complete(task1.getId());

        Task task2 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task2.getName() + "：任务节点");
        Map<String, Object> variables = new HashMap<>();
        Random random = new Random();
        int amount = random.nextInt(10) + 99995;
        System.out.println("====贷款额度：" + amount);
        variables.put("amount", amount);
        taskService.complete(task2.getId(), variables);

        while (true) {
            Task task3 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
            if (null == task3) {
                break;
            }
            System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task3.getName() + "：任务节点");
            //模拟超时
            if ("复核贷款额度".equals(task3.getName())) {
                System.out.println("====" + task3.getName() + "：任务节点等待审批中");
                try {
                    Thread.sleep(70 * 1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            Task task = taskService.createTaskQuery().taskId(task3.getId()).singleResult();
            if (null != task) {
                taskService.complete(task.getId());
            }
        }


        closeEngine();
    }
}
